import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/match/list/match_page_logic.dart';
import 'package:ball_lion/service/config_service.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/widgets/bottom_picker_widget.dart';
import 'package:ball_lion/widgets/base/wqs_widget.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class ZqConfigPage extends StatefulWidget {
  const ZqConfigPage({super.key});

  @override
  State<ZqConfigPage> createState() => _ZqConfigPageState();
}

class _ZqConfigPageState extends State<ZqConfigPage> {
  ConfigService configService = Get.find<ConfigService>();
  ZqConfig config = Get.find<ConfigService>().zqConfig;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(title: const QsText('足球比赛设置')),
      backgroundColor: Colours.scaffoldBg,
      body: VisibilityDetector(
        key: const Key('ZqConfigPage'),
        onVisibilityChanged: (info) {
          if (!info.visibleBounds.isEmpty) {
            update();
          }
        },
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 8),
          child: ListView(
            children: [
              separateTitle('应用内提醒'),
              QsWidget.cell('提醒范围', config.zqAlert1 == 0 ? '我关注的比赛' : '全部比赛',
                  onTap: () async {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '0'});
                // Get.toNamed(Routes.alertRangeConfig, arguments: 0);
                final result = await Get.bottomSheet(BottomPickerWidget(
                  ['我关注的比赛', '全部比赛'],
                  title: '提醒范围',
                  initialIndex: config.zqAlert1,
                ));
                if (result != null) {
                  if (config.zqAlert1 == result) {
                    return;
                  }
                  config.zqAlert1 = result;
                  update();
                  Get.find<ConfigService>()
                      .update(ConfigType.zqAlert1, config.zqAlert1);
                }
              }),
              const SizedBox(height: 10),
              jinqiu(),
              const SizedBox(height: 10),
              cell([
                title('红牌提示'),
                const Spacer(),
                selectItem('弹窗', config.zqAlert5.contains(0), onTap: () {
                  Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '4'});
                  if (config.zqAlert5.contains(0)) {
                    config.zqAlert5.clear();
                  } else {
                    config.zqAlert5.add(0);
                  }
                  update();
                  configService.update(ConfigType.zqAlert5, config.zqAlert5);
                }),
                const SizedBox(width: 12),
                selectItem('声音', config.zqAlert5.contains(1), onTap: () {
                  Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '4'});
                  if (config.zqAlert5.contains(1)) {
                    config.zqAlert5.remove(1);
                  } else {
                    config.zqAlert5.add(1);
                    config.zqAlert5.addIf(!config.zqAlert5.contains(0), 0);
                  }
                  update();
                  configService.update(ConfigType.zqAlert5, config.zqAlert5);
                }),
                const SizedBox(width: 12),
                selectItem('震动', config.zqAlert5.contains(2), onTap: () {
                  Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '4'});
                  if (config.zqAlert5.contains(2)) {
                    config.zqAlert5.remove(2);
                  } else {
                    config.zqAlert5.add(2);
                    config.zqAlert5.addIf(!config.zqAlert5.contains(0), 0);
                  }
                  update();
                  configService.update(ConfigType.zqAlert5, config.zqAlert5);
                }),
              ]),
              if (config.zqAlert2.contains(0) ||
                  config.zqAlert5.contains(0)) ...[
                const SizedBox(height: 10),
                cell([
                  title('弹窗范围'),
                  const Spacer(),
                  selectItem('比赛列表', config.zqAlert6 == 0, onTap: () {
                    Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '5'});
                    if (config.zqAlert6 == 0) return;
                    config.zqAlert6 = 0;
                    update();
                    configService.update(ConfigType.zqAlert5, config.zqAlert5);
                  }),
                  const SizedBox(width: 13),
                  selectItem('APP全局', config.zqAlert6 == 1, onTap: () {
                    Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '5'});
                    if (config.zqAlert6 == 1) return;
                    config.zqAlert6 = 1;
                    update();
                    configService.update(ConfigType.zqAlert5, config.zqAlert5);
                  }),
                ]),
              ],
              const SizedBox(height: 10),
              Styles.card(
                child: switchCell('全部、赛程、赛果频道共享筛选条件', config.zqAlert7 == 1,
                    ConfigType.zqAlert7),
              ),
              separateTitle('列表数据显示设置'),
              zhishu(),
              separateTitle('通知设置'),
              QsWidget.cell(
                  '关注比赛通知',
                  config.zqPush1 == 1 && configService.pushConfig.pushAll == 1
                      ? config.getPushConfigCount()
                      : '0/7', onTap: () {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '13'});
                Get.toNamed(Routes.zqPushConfig);
              }),
              const SizedBox(height: 50),
            ],
          ),
        ),
      ),
    );
  }

  Widget cell(List<Widget> children, {Function()? onTap, bool isCard = true}) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.opaque,
      child: Styles.card(
        height: 52,
        radius: isCard ? 8 : 0,
        color: Colours.white,
        padding: const EdgeInsets.symmetric(horizontal: 12),
        child: Row(children: children),
      ),
    );
  }

  Widget title(String title) {
    return QsText(title, fontSize: 16);
  }

  Widget separateTitle(String title) {
    return Padding(
      padding: const EdgeInsets.only(left: 12, top: 12, bottom: 6),
      child: QsText(title, color: Colours.grey66, fontSize: 13),
    );
  }

  Widget detail(String title) {
    return QsText(title, color: Colours.grey66, fontSize: 16);
  }

  Widget switchCell(String title, bool select, int id) {
    return Container(
      height: 52,
      color: Colours.white,
      padding: const EdgeInsets.only(left: 12, right: 12),
      child: Row(children: [
        QsText(title, fontSize: 16),
        const Spacer(),
        Transform.scale(
          scale: 0.9,
          child: CupertinoSwitch(
            value: select,
            // trackColor: Colours.blue2766D6,
            activeColor: Colours.main,
            onChanged: (value) {
              if (id == ConfigType.zqAlert7) {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '6'});
                config.zqAlert7 = (config.zqAlert7 == 1 ? 0 : 1);
                configService.update(id, config.zqAlert7);
              } else if (id == ConfigType.zqList2) {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '8'});
                config.zqList2 = (config.zqList2 == 1 ? 0 : 1);
                configService.update(id, config.zqList2);
              } else if (id == ConfigType.zqList3) {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '9'});
                config.zqList3 = (config.zqList3 == 1 ? 0 : 1);
                configService.update(id, config.zqList3);
              } else if (id == ConfigType.zqList4) {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '10'});
                config.zqList4 = (config.zqList4 == 1 ? 0 : 1);
                configService.update(id, config.zqList4);
              } else if (id == ConfigType.zqList5) {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '11'});
                config.zqList5 = (config.zqList5 == 1 ? 0 : 1);
                configService.update(id, config.zqList5);
              } else if (id == ConfigType.zqList6) {
                Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '12'});
                config.zqList6 = (config.zqList6 == 1 ? 0 : 1);
                configService.update(id, config.zqList6);
              }
              if (id != ConfigType.zqAlert7) {
                final matchController =
                    Get.find<MatchPageLogic>().getMatchController();
                matchController.updateView();
              }
              update();
            },
          ),
        )
      ]),
    );
  }

  Widget selectItem(String title, bool select, {Function()? onTap}) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: SizedBox(
        height: 40,
        child: Row(
          children: [
            Image.asset(
              Utils.getImgPath(
                  select ? 'config_check.png' : 'config_uncheck.png'),
              width: 22,
              height: 22,
            ),
            const SizedBox(width: 3),
            QsText(title, color: Colours.grey66)
          ],
        ),
      ),
    );
  }

  jinqiu() {
    return Styles.card(
      child: Column(children: [
        cell([
          title('进球提示'),
          const Spacer(),
          selectItem('弹窗', config.zqAlert2.contains(0), onTap: () {
            Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '1'});
            if (config.zqAlert2.contains(0)) {
              config.zqAlert2.clear();
            } else {
              config.zqAlert2.add(0);
            }
            update();
            configService.update(ConfigType.zqAlert2, config.zqAlert2);
          }),
          const SizedBox(width: 12),
          selectItem('声音', config.zqAlert2.contains(1), onTap: () {
            Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '1'});
            if (config.zqAlert2.contains(1)) {
              config.zqAlert2.remove(1);
            } else {
              config.zqAlert2.add(1);
              config.zqAlert2.addIf(!config.zqAlert2.contains(0), 0);
            }
            update();
            configService.update(ConfigType.zqAlert2, config.zqAlert2);
          }),
          const SizedBox(width: 12),
          selectItem('震动', config.zqAlert2.contains(2), onTap: () {
            Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '1'});
            if (config.zqAlert2.contains(2)) {
              config.zqAlert2.remove(2);
            } else {
              config.zqAlert2.add(2);
              config.zqAlert2.addIf(!config.zqAlert2.contains(0), 0);
            }
            update();
            configService.update(ConfigType.zqAlert2, config.zqAlert2);
          }),
        ], isCard: false),
        if (config.zqAlert2.contains(1)) ...[
          QsWidget.seperateLine(),
          QsWidget.cell('主队进球声音', configService.soundName[config.zqAlert3],
              isCard: false, onTap: () {
            Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '2'});
            Get.toNamed(Routes.zqSoundConfig, arguments: 0);
          }),
          QsWidget.seperateLine(),
          QsWidget.cell('客队进球声音', configService.soundName[config.zqAlert4],
              isCard: false, onTap: () {
            Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '3'});
            Get.toNamed(Routes.zqSoundConfig, arguments: 1);
          }),
        ]
      ]),
    );
  }

  zhishu() {
    return Styles.card(
      color: Colours.white,
      child: Column(children: [
        // if(AppConfig.config.showOdds && ModeConfig.matchListOdds)...[
        //   QsWidget.cell('指数', config.zqList1 == 1 ? '开启' : '关闭', onTap: () {
        //     Utils.onEvent('zqpd_szan', params: {'zqpd_szan': '7'});
        //     Get.toNamed(Routes.zqOddsConfig);
        //   }),
        //   const Divider(height: 0.5, color: Colours.greyEE, indent: 12)
        // ],
        switchCell('球队排名', config.zqList2 == 1, ConfigType.zqList2),
        const Divider(height: 0.5, color: Colours.greyEE, indent: 12),
        switchCell('彩种编号', config.zqList3 == 1, ConfigType.zqList3),
        // Divider(height: 0.5, color: Colours.greyEE, indent: 12),
        // switchCell('方案数量', config.zqList4 == 1, ConfigType.zqList4),
        const Divider(height: 0.5, color: Colours.greyEE, indent: 12),
        switchCell('红黄牌', config.zqList5 == 1, ConfigType.zqList5),
        const Divider(height: 0.5, color: Colours.greyEE, indent: 12),
        switchCell('角球', config.zqList6 == 1, ConfigType.zqList6)
      ]),
    );
  }
}
